/**
 * @(#) ModifyProfileMediator.as
 *
 * @date		2010. 10. 05.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	인사기록 수정 - Mediator
 * 				service ace 요청  개인 기록 수정 Mediator 
 */
package com.skt.common_hr.myprofile_stc.view
{
	import com.skt.common_hr.common.*;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.myprofile_stc.model.*;
	import com.skt.common_hr.myprofile_stc.model.vo.CareerVO;
	import com.skt.common_hr.myprofile_stc.model.vo.FamilyVO;
	import com.skt.common_hr.myprofile_stc.model.vo.ModLicenseVO;
	import com.skt.common_hr.myprofile_stc.model.vo.ScholarshipVO;
	import com.skt.common_hr.myprofile_stc.view.components.OverView;
	import com.skt.common_hr.myprofile_stc.view.components.ScholarshipView;
	import com.skt.common_hr.myprofile_stc.view.components.TabSheet1m;
	import com.skt.common_hr.myprofile_stc.view.components.TabSheet30m_Stc;
	import com.skt.common_hr.myprofile_stc.view.components.TabSheet3m_Stc;
	import com.skt.common_hr.myprofile_stc.view.components.TabSheet4m_Stc;
	import com.skt.common_hr.myprofile_stc.view.components.TabSheet5m_Stc;
	import com.skt.common_hr.myprofile_stc.view.components.TabSheet9m_Stc;
	import com.wcl.utils.DateUtil;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	

	public class ModifyProfileMediator extends CommonMediator
	{
		public static const NAME:String = "ModifyProfileMediator"; 
		public static const END_OF_MODIFY:String = "endOfModify";
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function ModifyProfileMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		





		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Override functions of CommonMediator
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		override protected function initializeView():void
 		{
 			view.tsm1.addEventListener(TabSheet1m.SAVE_PERSONAL, onSavePersonal);
 			view.tsm1.addEventListener(TabSheet1m.CANCEL_PERSONAL, onCancelPersonal);

 			view.tsm30.addEventListener(TabSheet30m_Stc.SAVE_CAREER, onSaveSkCareer);
 			view.tsm30.addEventListener(TabSheet30m_Stc.CANCEL_CAREER, onCancelSkCareer);
 			view.tsm30.addEventListener(TabSheet30m_Stc.ADD_RECORD, onAddRecordSkCareer);
 			view.tsm30.addEventListener(TabSheet30m_Stc.DEL_RECORD, onDelRecordSkCareer);

 			view.tsm3.addEventListener(TabSheet3m_Stc.SAVE_CAREER, onSaveCareer);
 			view.tsm3.addEventListener(TabSheet3m_Stc.CANCEL_CAREER, onCancelCareer);
 			view.tsm3.addEventListener(TabSheet3m_Stc.ADD_RECORD, onAddRecordCareer);
 			view.tsm3.addEventListener(TabSheet3m_Stc.DEL_RECORD, onDelRecordCareer);
 			
 			view.tsm4.addEventListener(TabSheet4m_Stc.SAVE_SCHOLARSHIP, onSaveScholarship);
 			view.tsm4.addEventListener(TabSheet4m_Stc.CANCEL_SCHOLARSHIP, onCancelScholarship);
 			view.tsm4.addEventListener(TabSheet4m_Stc.ADD_RECORD, onAddRecordScholarship);
 			view.tsm4.addEventListener(TabSheet4m_Stc.DEL_RECORD, onDelRecordScholarship);
 			
 			view.tsm5.addEventListener(TabSheet5m_Stc.SAVE_FAMILY, onSaveFamily);
 			view.tsm5.addEventListener(TabSheet5m_Stc.CANCEL_FAMILY, onCancelFamily);
 			view.tsm5.addEventListener(TabSheet5m_Stc.ADD_RECORD, onAddRecordFamily);
 			view.tsm5.addEventListener(TabSheet5m_Stc.DEL_RECORD, onDelRecordFamily);

 			view.tsm9.addEventListener(TabSheet9m_Stc.SAVE_LICENSE, onSaveLicense);
 			view.tsm9.addEventListener(TabSheet9m_Stc.CANCEL_LICENSE, onCancelLicense);
 			view.tsm9.addEventListener(TabSheet9m_Stc.ADD_RECORD, onAddRecordLicense);
 			view.tsm9.addEventListener(TabSheet9m_Stc.DEL_RECORD, onDelRecordLicense);

 			//initBasicData();
 			loadPersonal();
 			loadSkCareer();
 			loadCareer();
 			loadScholarship();
 			loadFamily();
 			loadLicense();
		}
 		
 		
		/**
		* View에 표현된 데이터들을 삭제한다.
		*	- view가 sleep 으로 들어갈때 실행됨..
		* @param
		* @return void
		*/
 		override protected function clearView():void
 		{
 		}
 		
 		
 		private function onAddRecordSkCareer(e:Event):void
 		{
 			var vo:CareerVO = new CareerVO();
 			view.tsm30.dataProvider.addItem(vo);
 		}

 		private function onAddRecordCareer(e:Event):void
 		{
 			var vo:CareerVO = new CareerVO();
 			view.tsm3.dataProvider.addItem(vo);
 		}
 		
 		private function onAddRecordScholarship(e:Event):void
 		{
 			var vo:ScholarshipVO = new ScholarshipVO();
 			view.tsm4.dataProvider.addItem(vo);
 		}
 		
 		private function onAddRecordFamily(e:Event):void
 		{
 			var vo:FamilyVO = new FamilyVO();
 			view.tsm5.dataProvider.addItem(vo);
 		}
 		
 		private function onAddRecordLicense(e:Event):void
 		{
 			var vo:ModLicenseVO = new ModLicenseVO();
 			view.tsm9.dataProvider.addItem(vo);
 		}
 		

 		private function onDelRecordSkCareer(e:Event):void
 		{
 			var dp:ArrayCollection = new ArrayCollection(view.tsm30.dataProvider.source);
 			
 			for(var i:int=0; i<dp.length; i++) {
				 if(dp.getItemAt(i).selected) {
				 	dp.removeItemAt(i);
				 	i--;
				 }
 			}
 			view.tsm30.dataProvider = dp;
 		}
 		
 		private function onDelRecordCareer(e:Event):void
 		{
 			var dp:ArrayCollection = new ArrayCollection(view.tsm3.dataProvider.source);
 			
 			for(var i:int=0; i<dp.length; i++) {
				 if(dp.getItemAt(i).selected) {
				 	dp.removeItemAt(i);
				 	i--;
				 }
 			}
 			view.tsm3.dataProvider = dp;
 		}

 		private function onDelRecordScholarship(e:Event):void
 		{
 			var dp:ArrayCollection = new ArrayCollection(view.tsm4.dataProvider.source);
 			
 			for(var i:int=0; i<dp.length; i++) {
				 if(dp.getItemAt(i).selected) {
				 	dp.removeItemAt(i);
				 	i--;
				 }
 			}
 			view.tsm4.dataProvider = dp;
 		}
 		
 		private function onDelRecordFamily(e:Event):void
 		{
 			var dp:ArrayCollection = new ArrayCollection(view.tsm5.dataProvider.source);
 			
 			for(var i:int=0; i<dp.length; i++) {
				 if(dp.getItemAt(i).selected) {
				 	dp.removeItemAt(i);
				 	i--;
				 }
 			}
 			view.tsm5.dataProvider = dp;
 		}
 		
 		private function onDelRecordLicense(e:Event):void
 		{
 			var dp:ArrayCollection = new ArrayCollection(view.tsm9.dataProvider.source);
 			
 			for(var i:int=0; i<dp.length; i++) {
				 if(dp.getItemAt(i).selected) {
				 	dp.removeItemAt(i);
				 	i--;
				 }
 			}
 			view.tsm9.dataProvider = dp;
 		}
 		
 		private function onSavePersonal(e:Event):void
 		{
 			var proxy:PersonalProxy = facade.retrieveProxy(PersonalProxy.NAME) as PersonalProxy;
 			proxy.save(Variables.myPernr, getPersonalData());
 		}
		
		private function getPersonalData():Object
		{
			var obj:Object = new Object();
			
			obj.BEGDA = DateUtil.toBasicString(view.tsm1.dataProvider.BEGDA);
			obj.ENDDA = DateUtil.toBasicString(view.tsm1.dataProvider.ENDDA);
			obj.FAMDT = DateUtil.toBasicString(view.tsm1.dfMarriage.selectedDate);
			obj.LNMCH = view.tsm1.txtLNMCH.text;
			obj.FNMCH = view.tsm1.txtFNMCH.text;
			obj.NACHN = view.tsm1.txtNACHN.text;
			obj.VORNA = view.tsm1.txtVORNA.text;
			obj.CTALL = view.tsm1.txtCTALL.text;
			obj.CWEIGHT = view.tsm1.txtCWEIGHT.text;
			obj.CBLOOD = view.tsm1.cbCBLOOD.text;
			obj.CARMY1 = view.tsm1.cbCARMY1.text;
			obj.CARMY2 = view.tsm1.cbCARMY2.text;
			obj.CGUBUN = view.tsm1.dataProvider.CGUBUN;
			obj.CGBDAT = DateUtil.toBasicString(view.tsm1.dfBirth.selectedDate);
			return obj;
		}
		
		
 		private function onSaveCareer(e:Event):void
 		{
 			var proxy:CareerProxy = facade.retrieveProxy(CareerProxy.NAME) as CareerProxy;
 			var saveArr:Array = new Array();
 			for each(var vo:CareerVO in view.tsm3.dataProvider) {
 				var obj:Object = proxy.toJSONobject(vo);
 				saveArr.push(obj);
 			}
 			proxy.save(Variables.myPernr, saveArr);
 		}
 		
 		private function onSaveSkCareer(e:Event):void
 		{
 			var proxy:SkCareerProxy = facade.retrieveProxy(SkCareerProxy.NAME) as SkCareerProxy;
 			var saveArr:Array = new Array();
 			for each(var vo:CareerVO in view.tsm30.dataProvider) {
 				var obj:Object = proxy.toJSONobject(vo);
 				saveArr.push(obj);
 			}
 			proxy.save(Variables.myPernr, saveArr);
 		}
 		
 		private function onSaveScholarship(e:Event):void
 		{
 			var proxy:ScholarshipProxy = facade.retrieveProxy(ScholarshipProxy.NAME) as ScholarshipProxy;
 			var saveArr:Array = new Array();
 			for each(var vo:ScholarshipVO in view.tsm4.dataProvider) {
 				var obj:Object = proxy.toJSONobject(vo);
 				saveArr.push(obj);
 			}
 			proxy.save(Variables.myPernr, saveArr);
 		}
 		
 		private function onSaveFamily(e:Event):void
 		{
 			var proxy:FamilyProxy = facade.retrieveProxy(FamilyProxy.NAME) as FamilyProxy;
 			var saveArr:Array = new Array();
 			for each(var vo:FamilyVO in view.tsm5.dataProvider) {
 				var obj:Object = proxy.toJSONobject(vo);
 				saveArr.push(obj);
 			}
 			proxy.save(Variables.myPernr, saveArr);
 		}
 		
 		private function onSaveLicense(e:Event):void
 		{
 			var proxy:LicenseProxy = facade.retrieveProxy(LicenseProxy.NAME) as LicenseProxy;
 			var saveArr:Array = new Array();
 			for each(var vo:ModLicenseVO in view.tsm9.dataProvider) {
 				var obj:Object = proxy.toJSONobject(vo);
 				saveArr.push(obj);
 			}
 			proxy.save(Variables.myPernr, saveArr);
 		}
 		
 		private function onCancelPersonal(e:Event):void {
 			view.currentState = "";
 			facade.sendNotification(END_OF_MODIFY, null);
 		}

 		private function onCancelSkCareer(e:Event):void {
 			view.currentState = "";
 			facade.sendNotification(END_OF_MODIFY, null);
 		}
 		
 		private function onCancelCareer(e:Event):void {
 			view.currentState = "";
 			facade.sendNotification(END_OF_MODIFY, null);
 		}
 		
 		private function onCancelScholarship(e:Event):void {
 			view.currentState = "";
 			facade.sendNotification(END_OF_MODIFY, null);
 		}
 		
 		private function onCancelFamily(e:Event):void {
 			view.currentState = "";
 			facade.sendNotification(END_OF_MODIFY, null);
 		}
 		
 		private function onCancelLicense(e:Event):void {
 			view.currentState = "";
 			facade.sendNotification(END_OF_MODIFY, null);
 		}
 		
 		
 		/**
		 * @param
		 * @return void
		 */
		private function loadPersonal():void
		{
			var personalProxy:PersonalProxy = facade.retrieveProxy(PersonalProxy.NAME) as PersonalProxy;
			personalProxy.load(Variables.myPernr);
		}
		
		private function loadSkCareer():void
		{
			var proxy:SkCareerProxy = facade.retrieveProxy(SkCareerProxy.NAME) as SkCareerProxy;
			proxy.load(Variables.myPernr);
		}
		
		
		private function loadCareer():void
		{
 			var careerProxy:CareerProxy = facade.retrieveProxy(CareerProxy.NAME) as CareerProxy;
 			careerProxy.load(Variables.myPernr);
 		}
		
		private function loadScholarship():void
		{
			var proxy:ScholarshipProxy = facade.retrieveProxy(ScholarshipProxy.NAME) as ScholarshipProxy;
			proxy.load(Variables.myPernr);
		} 
		
		private function loadFamily():void
		{
 			var proxy:FamilyProxy = facade.retrieveProxy(FamilyProxy.NAME) as FamilyProxy;
 			proxy.load(Variables.myPernr);
 		}
 
 		private function loadLicense():void
 		{
 			var proxy:LicenseProxy = facade.retrieveProxy(LicenseProxy.NAME) as LicenseProxy;
 			proxy.load(Variables.myPernr);
 		}
 		
 		private function saveCareer():void
 		{
 			var careerProxy:CareerProxy = facade.retrieveProxy(CareerProxy.NAME) as CareerProxy;
 			careerProxy.load(Variables.myPernr);
 		}
 		
 		private function saveScholarship():void
 		{
 			var scholarshipProxy:ScholarshipProxy = facade.retrieveProxy(ScholarshipProxy.NAME) as ScholarshipProxy;
 			scholarshipProxy.load(Variables.myPernr);
 		}
 		
 		private function saveLicense():void
 		{
 			var scholarshipProxy:ScholarshipProxy = facade.retrieveProxy(ScholarshipProxy.NAME) as ScholarshipProxy;
 			scholarshipProxy.load(Variables.myPernr);
 		}
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					PersonalProxy.LOAD_PERSONAL_COMPLETE,
					PersonalProxy.LOAD_PERSONAL_FAILED,
					PersonalProxy.SAVE_PERSONAL_COMPLETE,
					PersonalProxy.SAVE_PERSONAL_FAILED,
					SkCareerProxy.LOAD_SKCAREER_COMPLETE,
					SkCareerProxy.LOAD_SKCAREER_FAILED,
					SkCareerProxy.SAVE_SKCAREER_COMPLETE,
					SkCareerProxy.SAVE_SKCAREER_FAILED,		
					CareerProxy.LOAD_CAREER_COMPLETE,
					CareerProxy.LOAD_CAREER_FAILED,
					ScholarshipProxy.LOAD_SCHOLARSHIP_COMPLETE,
					ScholarshipProxy.LOAD_SCHOLARSHIP_FAILED,
					ScholarshipProxy.SAVE_SCHOLARSHIP_COMPLETE,
					ScholarshipProxy.SAVE_SCHOLARSHIP_FAILED,
					FamilyProxy.LOAD_FAMILY_COMPLETE,
					FamilyProxy.LOAD_FAMILY_FAILED,
					FamilyProxy.SAVE_FAMILY_COMPLETE,
					FamilyProxy.SAVE_FAMILY_FAILED,
					LicenseProxy.LOAD_LICENSE_COMPLETE,
					LicenseProxy.LOAD_LICENSE_FAILED,
					LicenseProxy.SAVE_LICENSE_COMPLETE,
					LicenseProxy.SAVE_LICENSE_FAILED,
					CareerProxy.SAVE_CAREER_COMPLETE,
					CareerProxy.SAVE_CAREER_FAILED			
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			
			switch(note.getName()) 
			{
				case PersonalProxy.LOAD_PERSONAL_COMPLETE:
					onLoadPersonal(note.getBody() as PersonalProxy);
					break;
				case PersonalProxy.LOAD_PERSONAL_FAILED:
					CommonAlert.showMessage("", "개인신상 정보를 로드중 오류가 발생되었습니다.")
					break;
				case PersonalProxy.SAVE_PERSONAL_COMPLETE:
					loadPersonal();
					CommonAlert.showMessage("", "개인신상 정보가 정상적으로 저장되었습니다.")
					break;
				case PersonalProxy.SAVE_PERSONAL_FAILED:
					/* sap에서 리턴해주는 에러text 보여주기위해 주석처리 */
					//CommonAlert.showMessage("", "개인신상 정보를 저장중 오류가 발생되었습니다.")
					CommonAlert.showMessage("", (note.getBody() as PersonalProxy).retText)
					break;
				case SkCareerProxy.LOAD_SKCAREER_COMPLETE:
					onLoadSkCareer(note.getBody() as SkCareerProxy);
					break;
				case SkCareerProxy.LOAD_SKCAREER_FAILED:
					CommonAlert.showMessage("", "SK OS 경력 정보를 로드중 오류가 발생되었습니다.")
					break;
				case SkCareerProxy.SAVE_SKCAREER_COMPLETE:
					CommonAlert.showMessage("", "SK OS 경력 정보가 정상적으로 저장되었습니다.")
					break;
				case SkCareerProxy.SAVE_SKCAREER_FAILED:
					CommonAlert.showMessage("", (note.getBody() as SkCareerProxy).retText)
					//CommonAlert.showMessage("", "SK OS 경력 정보를 저장중 오류가 발생되었습니다.")
					break;
				case FamilyProxy.LOAD_FAMILY_COMPLETE:
					onLoadFamily(note.getBody() as FamilyProxy);
					break;
				case FamilyProxy.LOAD_FAMILY_FAILED:
					CommonAlert.showMessage("", "가족사항을 로드중 오류가 발생되었습니다.")
					break;
				case FamilyProxy.SAVE_FAMILY_COMPLETE:
					//loadFamily();
					CommonAlert.showMessage("", "가족사항이 정상적으로 저장되었습니다.")
					break;
				case FamilyProxy.SAVE_FAMILY_FAILED:
					CommonAlert.showMessage("", (note.getBody() as FamilyProxy).retText)
					break;
				case LicenseProxy.LOAD_LICENSE_COMPLETE:
					onLoadLicense(note.getBody() as LicenseProxy);
					break;
				case LicenseProxy.LOAD_LICENSE_FAILED:
					CommonAlert.showMessage("", "자격/면허 정보를 로드중 오류가 발생되었습니다.")
					break;
				case LicenseProxy.SAVE_LICENSE_COMPLETE:
					loadLicense();
					CommonAlert.showMessage("", "자격/면허 정보가 정상적으로 저장되었습니다.")
					break;
				case LicenseProxy.SAVE_LICENSE_FAILED:
					CommonAlert.showMessage("", (note.getBody() as LicenseProxy).retText)
					break;
				case CareerProxy.LOAD_CAREER_COMPLETE:
					loadLicense();
					onLoadCareer(note.getBody() as CareerProxy);
					break;
				case ScholarshipProxy.LOAD_SCHOLARSHIP_COMPLETE:
					onLoadScholarship(note.getBody() as ScholarshipProxy);
					break;
				case ScholarshipProxy.LOAD_SCHOLARSHIP_FAILED:
					CommonAlert.showMessage("", "학력사항 조회중 오류가 발생되었습니다.")
					break;
				case ScholarshipProxy.SAVE_SCHOLARSHIP_COMPLETE:
					//loadScholarship();
					CommonAlert.showMessage("", "학력사항이  정상적으로 저장되었습니다.")
					break;
				case ScholarshipProxy.SAVE_SCHOLARSHIP_FAILED:
					CommonAlert.showMessage("", (note.getBody() as ScholarshipProxy).retText)
					break;
				case CareerProxy.SAVE_CAREER_COMPLETE:
					//loadScholarship();
					CommonAlert.showMessage("", "경력사항 정보가 정상적으로 저장되었습니다.")
					break;
				case CareerProxy.SAVE_CAREER_FAILED:
					CommonAlert.showMessage("", (note.getBody() as CareerProxy).retText)
					break;	
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}

		private function onLoadPersonal(proxy:PersonalProxy):void
		{
			view.tsm1.dataProvider = proxy.personal;		
		}
		
		private function onLoadSkCareer(proxy:SkCareerProxy):void
		{
			view.tsm30.dataProvider = proxy.careerList;
			view.tsm30.codeList1 = proxy.codeList1;
			view.tsm30.codeList2 = proxy.codeList2;
		}
		
		private function onLoadScholarship(proxy:ScholarshipProxy):void
		{
			view.tsm4.schoolLevels = proxy.schoolLevel;
			view.tsm4.codeList2 = proxy.codeList2;
			view.tsm4.dataProvider = proxy.scholarshipList;
		}
		
		private function onLoadCareer(proxy:CareerProxy):void
		{
			view.tsm3.dataProvider = proxy.careerList;
		}

		private function onLoadFamily(proxy:FamilyProxy):void
		{
			view.tsm5.dataProvider = proxy.familyList;
			view.tsm5.codeList	   = proxy.codeList;
		}
		
		private function onLoadLicense(proxy:LicenseProxy):void
		{
			view.tsm9.dataProvider = proxy.licenseList;
		}





		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	private functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private var modeModify:Boolean = false;
		private var viewScholarship:ScholarshipView= null;
		

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():OverView
        {
            return viewComponent as OverView;
        }
	}
}